<template>
  <div>
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup name="applyInvoice">
import { BasicTable, TableAction, ActionItem } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { useMessage } from '@apps/antd-admin/src/hooks/web/useMessage';
import { columns, searchFormSchema } from './data';
import { Api, updateInvoiceState } from './api';

const [registerDrawer, { openDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'applyInvoice-template',
  tableProps: {
    //表格标题
    title: '发票申请列表',
    //主键字段
    rowKey: 'applyInvoiceId',
    //列表接口
    listUrl: Api.page,
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      showExportButton: true,
    },
    //操作列配置
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;
const { createConfirm } = useMessage();

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '确认开票',
      onClick: handleOpen.bind(null, record.applyInvoiceId, 1),
      disabled: record.invoiceState == 1 || record.invoiceState == 2,
    },
    {
      label: '作废发票',
      onClick: handleInvalid.bind(null, record.applyInvoiceId, 2),
      disabled: record.invoiceState == 1 || record.invoiceState == 2,
    },
  ];
}

function handleInvalid(applyInvoiceId, invoiceState) {
  createConfirm({
    iconType: 'info',
    title: '作废发票',
    content: '确认作废该发票吗？',
    onOk: async () => {
      await updateInvoiceState({ applyInvoiceId: applyInvoiceId, invoiceState: invoiceState });
      reload();
    },
  });
}

function handleOpen(applyInvoiceId, invoiceState) {
  createConfirm({
    iconType: 'info',
    title: '确认开票',
    content: '确认已开发票吗？',
    onOk: async () => {
      await updateInvoiceState({ applyInvoiceId: applyInvoiceId, invoiceState: invoiceState });
      reload();
    },
  });
}

function handleExport() {}
</script>
